<script setup>
import {ref} from "vue";
import {login} from "@/api/auth/index.js";
import {setToken} from "@/utils/token/index.js";
import {searchSelfRouter, searchSelfInfo} from "@/api/user/index.js";
import {useMenuStore} from "@/stores/menu.js";
import {useRouter} from "vue-router";
import {useUserStore} from "@/stores/user.js";


const router = useRouter()
const menuStore = useMenuStore()
const userStore = useUserStore()

const loginForm = ref({
  account: undefined,
  password: undefined,
  rememberMe: undefined
})

function handleLogin() {
  login(loginForm.value).then((res) => {
    console.log(res.data)
    if (res.data.code === 200) {
      console.log("登录成功", res.data)
      setToken("daocaoToken", res.data.token)
      searchSelfRouter().then(res => {
        console.log("res: ", res);
        if (res.data.code === 200) {
          menuStore.setMenuList(res.data.data)
          router.push("/index")
        }
      })
      searchSelfInfo().then(res => {
        if (res.data.code === 200) {
          console.log("save res", res.data)
          userStore.setUserInfo(res.data.data)
        }
      })
    }
  })
}

</script>

<template>
  <div class="login_container">
    <div class="welcome-title">
      <div style="font-size: 30px; font-weight: bold;">欢迎使用稻草系统</div>
      <div style="margin-top: 10px;">基于Spring + Vue编写</div>
      <div style="margin-top: 5px;">欢迎各位同性学习</div>
    </div>
    <div class="login_form">
      <h2 class="title">欢迎登录</h2>
      <el-form ref="form" :model="loginForm">
        <el-form-item>
          <el-input v-model="loginForm.account" placeholder="请输入账号">
            <template #prefix>
              <el-icon class="el-input__icon">
                <User/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码">
            <template #prefix>
              <el-icon class="el-input__icon">
                <Lock/>
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <div class="login_additional">
          <el-checkbox v-model="loginForm.rememberMe" label="记住我" size="large"/>
          <el-text type="primary">忘记密码</el-text>
        </div>
        <el-divider>其它登录方式</el-divider>
        <div class="login_other">
        </div>
      </el-form>
      <el-button type="primary" round size="large" @click="handleLogin">登录</el-button>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_container {
  background-image: url("../assets/bgimg/1.png");
  background-size: 100%;
  height: 100vh;
  display: flex;
  justify-content: flex-end;
}

.welcome-title {
  position: absolute;
  bottom: 30px;
  left: 30px;
  color: white;
  text-shadow: 0 0 10px black;
}

.login_form {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 38%;
  background: #ffffff;
}

.title {
  margin-bottom: 30px;
}

.login_additional {

  display: flex;
  justify-content: space-between;
  align-items: center;
}

.login_other {
}

.el-form {
  width: 45%;
}
</style>